package com.kaibes.module.account.listener;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.kaibes.core.base.util.ObjectUtils;
import com.kaibes.module.account.core.AccountService;
import com.kaibes.module.account.pojo.AccountInsert;
import com.kaibes.module.user.UserService;
import com.kaibes.orm.core.listener.InsertListener;
import com.kaibes.orm.curd.Insert;
import com.kaibes.orm.spring.OrmListener;

@OrmListener(service = AccountService.class)
public class AccountInsertListener implements InsertListener<Integer> {
    
    @Autowired
    private UserService userService;
    
    @Override
    public void beforeInsert(Insert insert) throws Exception {
        if (userService != null) {
            List<Object> tempList = insert.getDatas();
            List<Object> dataList = new ArrayList<>();
            for (Object temp : tempList) {
                AccountInsert data = new AccountInsert();
                ObjectUtils.copyProperties(temp, data);
                data.setUserId(userService.createDefaultUser());
                dataList.add(data);
            }
            insert.setDatas(dataList);
        }
    }

}
